<template>
    <el-form :model="form" ref="formRef" class="p-20">
      <div class="item" v-for="(item, index) in form.items" :key="index">
        <el-descriptions :column="2" border :colon="true" class="chadche">
          <el-descriptions-item class-name="hidden" label-class-name="hidden" />
          <el-descriptions-item class-name="hidden" label-class-name="hidden" />
  
          <el-descriptions-item :span="2">
            <template #label>
              <span class="require">地区</span>
            </template>
            <area-cascader
              :strictly="true"
              :prop="'items.' + index + '.dizhi'"
              width="100%"
              v-model="item.dizhi"
              :disabledLevel="0"
              :maxLevel="2"
            />
          </el-descriptions-item>
          <el-descriptions-item :span="1">
            <template #label>
              <span class="require">是否财政直管</span>
            </template>
            <el-form-item
              :prop="'items.' + index + '.isVasaRecta'"
              :rules="$formRules.select"
            >
              <pub-dict-select
                style="width: 100%"
                v-model="item.isVasaRecta"
                dict-code="SF"
              />
            </el-form-item>
          </el-descriptions-item>
          <el-descriptions-item :span="1">
            <template #label>
              <span class="require">是否重点老区苏区</span>
            </template>
            <el-form-item
              :prop="'items.' + index + '.keyAreaCode'"
              :rules="$formRules.select"
            >
              <pub-dict-select
                style="width: 100%"
                v-model="item.keyAreaCode"
                dict-code="ZDLQSQ"
              />
            </el-form-item>
          </el-descriptions-item>
        </el-descriptions>
        <div class="plus-btn">
          <el-button
            v-if="index == form.items.length - 1"
            type="default"
            :icon="Plus"
            @click="addItem"
          />
          <el-button
            v-else
            type="default"
            :icon="Minus"
            @click="removeItem(item)"
          />
        </div>
      </div>
    </el-form>
    <div class="bottom-btn">
      <el-button type="default" size="small" @click="emit('cancel')">
        取消
      </el-button>
      <el-button type="primary" size="small" @click="submitForm(1)">
        提交
      </el-button>
    </div>
  </template>
  <script setup>
  import { reactive, ref } from '@vue/reactivity'
  import { onMounted } from '@vue/runtime-core'
  import { ElMessage } from 'element-plus'
  import { useUserStore } from '@/pinia/modules/user'
  import { Delete, Edit, Minus, Plus } from '@element-plus/icons-vue'
  import { add ,get_detail} from '@/api/ZJGL/lqsqwh'
  import uuid from 'uuid'
  const emit = defineEmits(['cancel'])
  
  const formRef = ref(null)
  const orgInfo = useUserStore().orgInfo
  const form = reactive({
    items: [
      {
        isVasaRecta: '',
        dizhi: [],
        cityCode:'',
        countyCode: '',
        keyAreaCode: '',
        uuid: uuid()
      }
    ]
  })
  const addItem = () => {
    form.items.push({
      isVasaRecta: '',
      dizhi: [],
      cityCode:'',
      countyCode: '',
      keyAreaCode: '',
      uuid: uuid()
    })
  }
  const removeItem = item => {
    form.items = form.items.filter(target => target.uuid != item.uuid)
  }
  const initForm = async id => {
    let res = await get_detail(id)
    debugger
    form.value = res.data
  }
  const submitForm = status => {
    formRef.value.validate(async valid => {
      if (valid) {
        //   新增还是编辑
        // let res = props.editId
        //   ? await warehouses_edit(form.value)
        //   : await warehouses_add(form.value)
        // if (res.succeed) {
        //   ElMessage.success(res.msg)
        //   emit('cancel')
        //   emit('success')
        // }
        form.items.map(item => {
          item.cityCode = item.dizhi[1]
          item.countyCode = item.dizhi[2]
        })
        add(form.items).then(res=>{
          if(res.succeed){
            ElMessage.success(res.msg)
            emit('success')
            emit('cancel')
          }
        })
      }
    })
  }
  </script>
  <style lang="scss" scoped>
  .item {
    display: flex;
    margin-top: 10px;
    &:first-child {
      margin-top: 0;
    }
    .plus-btn {
      width: 80px;
      display: flex;
      align-items: center;
      justify-content: center;
    }
    .chadche {
      flex: 1;
    }
  }
  </style>
  